package org.me.SendSMS.utli;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.me.SendSMS.data.Message;
import org.me.SendSMS.data.MessageType;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.content.Context;
import android.util.Log;


public class XMLUtli {
	
	private static InputStream 				inputStream;
	private static Document 				doc;
	private static DocumentBuilder 			db;
	private static DocumentBuilderFactory 	dbf;

	
	public static List<Message> parserXML(Context mContext)
	{
		try
		{
			inputStream = mContext.getAssets().open("xml/list_message.xml");
			dbf 		= DocumentBuilderFactory.newInstance();
		    db 			= dbf.newDocumentBuilder();
		    doc 		= db.parse(inputStream);		    
		    doc.getDocumentElement().normalize();
		    NodeList nodeLst = doc.getElementsByTagName("items");
		    
			List<Message> message = new ArrayList<Message>();
			
			
			for (int i = 0; i < nodeLst.getLength(); i++)
		    {
	      	    Node note = nodeLst.item(i);      	    
	      	    if (note.getNodeType() == Node.ELEMENT_NODE)
	      	    {
	      	    	
	      	    	//id
		      	    	Element IdElement 		= (Element) note;
		      	    	NodeList noteId 		= IdElement.getElementsByTagName("id");
		      	    	Element id 				= (Element) noteId.item(0);
		      	    	NodeList nodelistId 	= id.getChildNodes();
		      	    	
		      	    	//Title
		      	    	Element titleElement 	= (Element) note;
		      	    	NodeList noteTitle 		= titleElement.getElementsByTagName("title");
		      	    	Element title 			= (Element) noteTitle.item(0);
		      	    	NodeList nodelistTitle 	= title.getChildNodes();
		      	    	
		      	    	
		    	        String ID 				= ((Node) nodelistId.item(0)).getNodeValue();
		    	        String TITLE 			= ((Node) nodelistTitle.item(0)).getNodeValue();
		    	        
		    	        Log.e("parserXML", "ID = "+ID);
		    	        Log.e("parserXML", "TITLE = "+TITLE);
		    	        
		    	        message.add(new Message(ID,TITLE));
	      	    }
		    }	
			return message;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
	
	public static List<MessageType> parserXMLMessageType(Context mContext, String filename)
	{
		try
		{
			inputStream = mContext.getAssets().open("xml/"+filename+".xml");
			dbf 		= DocumentBuilderFactory.newInstance();
		    db 			= dbf.newDocumentBuilder();
		    doc 		= db.parse(inputStream);		    
		    doc.getDocumentElement().normalize();
		    NodeList nodeLst = doc.getElementsByTagName("items");
		    
			List<MessageType> messageType = new ArrayList<MessageType>();
			
			
			for (int i = 0; i < nodeLst.getLength(); i++)
		    {
	      	    Node note = nodeLst.item(i);      	    
	      	    if (note.getNodeType() == Node.ELEMENT_NODE)
	      	    {
	      	    	
		      	    	//Title
		      	    	Element titleElement 	= (Element) note;
		      	    	NodeList noteTitle 		= titleElement.getElementsByTagName("title");
		      	    	Element title 			= (Element) noteTitle.item(0);
		      	    	NodeList nodelistTitle 	= title.getChildNodes();
		      	    	
		      	    	
		    	        String TITLE 			= ((Node) nodelistTitle.item(0)).getNodeValue();
		    	        
		    	        Log.e("parserXML", "TITLE = "+TITLE);
		    	        
		    	        messageType.add(new MessageType(TITLE));
	      	    }
		    }	
			return messageType;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
	
	
	
	

}
